home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <events.h>
-
- #include "tcpglue.h"
-
- static TCPiopb TCPpb;
-
- static xCleanup(status) int status;
- {
- fprintf(stderr,"exiting status %d\n",status);
- (void) xTCPRelease(&TCPpb);
- }
-
- /*
- * TCP asynchronous notification routine
- */
- static int notified = 0;
- static int lastNotifyCount = 0;
-
- static StreamPtr notifyTcpStream;
- static unsigned short notifyEventCode;
- static Ptr notifyUserDataPtr;
- static unsigned short notifyTerminReason;
- static struct ICMPReport *notifyIcmpMsg;
-
- pascal void TCPNotify(tcpStream,eventCode,userDataPtr,terminReason,icmpMsg)
- StreamPtr tcpStream;
- unsigned short eventCode;
- Ptr userDataPtr;
- unsigned short terminReason;
- struct ICMPReport *icmpMsg;
- {
- notified++;
-
- notifyTcpStream = tcpStream;
- notifyEventCode = eventCode;
- notifyUserDataPtr = userDataPtr;
- notifyTerminReason = terminReason;
- notifyIcmpMsg = icmpMsg;
- }
-
- static char *eventNames[] =
- {
- "event 0",
- "closing",
- "ULP timeout",
- "terminate",
- "data arrival",
- "urgent data",
- "ICMP message"
- };
- static char *terminateReasons[] =
- {
- "reason 0",
- "reason 1",
- "remote abort",
- "network failure",
- "security/precedence mismatch",
- "ULP timeout",
- "ULP abort",
- "ULP close",
- "service failure"
- };
- static char *icmpMessages[] =
- {
- "net unreachable",
- "host unreachable",
- "protocol unreachable",
- "port unreachable",
- "fragmentation required",
- "source route failed",
- "time exceeded",
- "parameter problem",
- "missing required option"
- };
- checkNotify()
- {
- if (notified == lastNotifyCount)
- return;
-
- lastNotifyCount = notified;
- fprintf(stderr,"notify count is now %d\n",lastNotifyCount);
- fprintf(stderr,"stream %08x\n",notifyTcpStream);
- fprintf(stderr,"event %d '%s'\n",notifyEventCode,eventNames[notifyEventCode]);
- if (notifyEventCode == TCPTerminate)
- fprintf(stderr,"reason %d '%s'\n",notifyTerminReason,terminateReasons[notifyTerminReason]);
- if (notifyEventCode == TCPDataArrival)
- fprintf(stderr,"%d bytes\n",notifyTerminReason/*!?*/);
- fprintf(stderr,"icmp msg %08x\n",notifyIcmpMsg);
- if (notifyEventCode == TCPICMPReceived)
- {
- fprintf(stderr,"stream %08x\n",notifyIcmpMsg->streamPtr);
- fprintf(stderr,"local %08x/%d\n",notifyIcmpMsg->localHost,notifyIcmpMsg->localPort);
- fprintf(stderr,"remote %08x/%d\n",notifyIcmpMsg->remoteHost,notifyIcmpMsg->remotePort);
- fprintf(stderr,"%s\n",icmpMessages[notifyIcmpMsg->reportType]);
- fprintf(stderr,"optionalAddlInfo %04x\n",notifyIcmpMsg->optionalAddlInfo);
- fprintf(stderr,"optionalAddlInfoPtr %08x\n",notifyIcmpMsg->optionalAddlInfoPtr);
- }
- fprintf(stderr,"userdata %s\n",notifyUserDataPtr);
- }
-
- /*
- * TCP io completion routine
- */
- static OSErr TCPResult;
- static long rhost;
- static short rport;
- static int len;
-
- static void TCPDone(pb)
- struct TCPiopb *pb;
- {
- TCPResult = pb->ioResult;
- if (TCPResult != noErr)
- return;
-
- switch (pb->csCode)
- {
- case TCPPassiveOpen:
- rhost = pb->csParam.open.remoteHost;
- rport = pb->csParam.open.remotePort;
- break;
- case TCPRcv:
- len = pb->csParam.receive.rcvBuffLen;
- break;
- }
- }
-
- static OSErr io;
- static char rcvbuf[1024];
- static char sendbuf[1024];
-
- main()
- {
- atexit(xCleanup);
- /*
- fprintf(stderr,"address %08x\n",xIPAddr());
- fprintf(stderr,"netmask %08x\n",xNetMask());
- fprintf(stderr,"max mtu %d\n",xMaxMTU());
- */
- if ((io = xTCPCreate(8192,TCPNotify,&TCPpb)) != noErr)
- {
- fprintf(stderr,"TCPCreate failed code %d\n",io);
- exit(1);
- }
- fprintf(stderr,"tcpStream %08x\n",TCPpb.tcpStream);
-
- doPassiveOpen();
-
- fprintf(stderr,"notify spin with count %d\n",lastNotifyCount);
- for(;;)
- {
- checkNotify();
- }
-
- if ((io = xTCPRelease(&TCPpb)) != noErr)
- {
- fprintf(stderr,"xTCPRelease failed code %d\n",io);
- exit(1);
- }
- }
-
- doActiveOpen()
- {
- TCPResult = 1;
- if ((io = xTCPActiveOpen(&TCPpb,0,0x80646682,99,TCPDone)) != noErr)
- {
- fprintf(stderr,"xTCPActiveOpen failed code %d\n",io);
- exit(1);
- }
- fprintf(stderr,"about to spin on xTCPActiveOpen\n");
- while (TCPResult == 1)
- {
- checkNotify();
- }
- fprintf(stderr,"xTCPActiveOpen spin done\n");
- if (TCPResult != noErr)
- {
- fprintf(stderr,"xTCPActiveOpen failed code %d\n",TCPResult);
- /*
- exit(1);
- */
- }
- }
-
- doRvc()
- {
- TCPResult = 1;
- if ((io = xTCPRcv(&TCPpb,rcvbuf,sizeof(rcvbuf)-1,0/*infinity*/,TCPDone)) != noErr)
- {
- fprintf(stderr,"xTCPRcv failed code %d\n",io);
- exit(1);
- }
- fprintf(stderr,"about to spin on xTCPRcv\n");
- while (TCPResult == 1)
- {
- /* handle user i/f events */
- }
- fprintf(stderr,"xTCPRcv spin done\n");
- if (TCPResult != noErr)
- {
- fprintf(stderr,"xTCPRcv failed code %d\n",TCPResult);
- exit(1);
- }
- rcvbuf[len] = '\0';
- fprintf(stderr,"xTCPRcv'd %d bytes\n'%s'\n",len,rcvbuf);
- }
-
- doSend()
- {
- TCPResult = 1;
- strcpy(sendbuf,"HELO milligan.utcs.utoronto.ca");
- wds[0].length = strlen(sendbuf);
- wds[0].ptr = sendbuf;
- wds[1].length = 0;
- if ((io = xTCPSend(&TCPpb,wds,false,false,0)) != noErr)
- {
- fprintf(stderr,"xTCPSend failed code %d\n",io);
- exit(1);
- }
- #ifdef ASYNC
- fprintf(stderr,"about to spin on xTCPSend\n");
- while (TCPResult == 1)
- {
- /* handle user i/f events */
- }
- fprintf(stderr,"xTCPSend spin done\n");
- if (TCPResult != noErr)
- {
- fprintf(stderr,"xTCPSend failed code %d\n",TCPResult);
- exit(1);
- }
- #endif ASYNC
- }
-
- doPassiveOpen()
- {
- TCPResult = 1;
- if ((io = xTCPPassiveOpen(&TCPpb,25,0/*infinity*/,TCPDone)) != noErr)
- {
- fprintf(stderr,"xTCPPassiveOpen failed code %d\n",io);
- exit(1);
- }
- fprintf(stderr,"about to spin on xTCPPassiveOpen\n");
- while (TCPResult == 1)
- {
- /* handle user i/f events */
- }
- fprintf(stderr,"xTCPPassiveOpen spin done\n");
- if (TCPResult != noErr)
- {
- fprintf(stderr,"xTCPPassiveOpen failed code %d\n",TCPResult);
- exit(1);
- }
- fprintf(stderr,"remote %08x/%d\n",rhost,rport);
- }
-